package com.flow.modules.flowable.service.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flow.common.utils.Result;
import com.flow.modules.flowable.entity.Departments;
import com.flow.modules.flowable.entity.Users;
import com.flow.modules.flowable.service.*;
import com.flow.modules.flowable.vo.OrgTreeVo;
import com.flow.modules.sys.dto.SysDeptDTO;
import com.flow.modules.sys.entity.SysDeptEntity;
import com.flow.modules.sys.entity.SysUserEntity;
import com.flow.modules.sys.service.SysDeptService;
import com.flow.modules.sys.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : willian fu
 * @version : 1.0
 */
@Service
public class OrgUserAndDeptServiceImpl implements OrgUserAndDeptService {

//    @Resource
//    private DepartmentsService departmentsService;

//    @Resource
//    private UserService userService;

    @Resource
    private SysDeptsService sysDeptsService;

    @Resource
    private SysUsersService sysUsersService;

    /**
     * 查询组织架构树
     *
     * @param deptId    部门id
     * @return 组织架构树数据
     */
    @Override
    public Object getOrgTreeData(Long deptId, String type) {
        LambdaQueryWrapper<SysUserEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(deptId)) {
            LambdaQueryWrapper<SysDeptEntity> sysDeptEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            sysDeptEntityLambdaQueryWrapper.eq(SysDeptEntity::getPid, deptId);

            lambdaQueryWrapper.eq(SysUserEntity::getDeptId,deptId);


            List<OrgTreeVo> orgTreeVos = new LinkedList<>();

            sysDeptsService.list(sysDeptEntityLambdaQueryWrapper).forEach(dept -> {
                orgTreeVos.add(OrgTreeVo.builder()
                        .id(dept.getId())
                        .name(dept.getName())
                        .selected(false)
                        .type("dept")
                        .build());
            });
            sysUsersService.list(lambdaQueryWrapper).forEach(user -> {
                    orgTreeVos.add(OrgTreeVo.builder()
                        .id(user.getId())
                        .name(user.getRealName())
                        .avatar(user.getHeadUrl())
//                        .sex(user.getGender())
                        .type("user")
                        .selected(false)
                        .build());
            });
            return new Result<List<OrgTreeVo>>().ok(orgTreeVos);
        }

        List<SysUserEntity> users = sysUsersService.list();
        List<SysDeptEntity> departments = sysDeptsService.list();
        //将人员按部门归类分组
        Map<Long, List<SysUserEntity>> deptUsers = new HashMap<>();
        users.forEach(user -> {
//            for (String did : user.getDepartmentIds().split(",")) {
                List<SysUserEntity> usersList = deptUsers.get(user.getDeptId());
                if (null == usersList) {
                    List<SysUserEntity> list = new ArrayList<>();
                    list.add(user);
                } else {
                    usersList.add(user);
                }
//            }
        });
        //将部门及员工进行转换嵌套树形结构
        departments.forEach(dept -> {

        });
        return new Result<List>().ok(Collections.EMPTY_LIST);
    }

    /**
     * 模糊搜索用户
     *
     * @param userName 用户名/拼音/首字母
     * @return 匹配到的用户
     */
    @Override
    public Object getOrgTreeUser(String userName) {
        LambdaQueryWrapper<SysUserEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();

//        if (Validator.isChinese(userName)) {
            lambdaQueryWrapper.like(SysUserEntity::getRealName, "%" + userName + "%");
//        } else {
//            lambdaQueryWrapper.like(SysUserEntity::getPingyin, "%" + userName + "%");
//        }
        List<OrgTreeVo> list = new LinkedList<>();
        sysUsersService.list(lambdaQueryWrapper).forEach(user -> {
            list.add(OrgTreeVo.builder().type("user")
//                    .sex(user.getSex())
                    .avatar(user.getHeadUrl())
                    .name(user.getRealName())
                    .id(user.getId())
                    .selected(false).build());
        });
        return new Result<List<OrgTreeVo>>().ok(list);
    }
}
